<!DOCTYPE html>
<html lang="en">
	<head>
		<title>Autoload - Wave Framework</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width"/> 
		<link type="text/css" href="../style.css" rel="stylesheet" media="all"/>
		<link rel="icon" href="../../favicon.ico" type="image/x-icon"/>
		<link rel="icon" href="../../favicon.ico" type="image/vnd.microsoft.icon"/>
	</head>
	<body>
	
		<h1>Autoload</h1>
		
			<ul>
				<li><a href="#index-files">Files</a></li>
				<li><a href="#index-introduction">Introduction</a></li>
				<li><a href="#index-using-autoload-script-for-javascript">Using Autoload Script for JavaScript</a></li>
				<li><a href="#index-using-autoload-script-with-php">Using Autoload Script with PHP</a></li>
			</ul>
		
			<h2 id="index-files">Files</h2>
			
				<h3>/resources/scripts/script.php</h3>
				<h3>/resources/scripts/script.js</h3>
		
			<h2 id="index-introduction">Introduction</h2>
			
				<p>The script.js and script.php files in '/resources/scripts/' folder are automatically loaded scripts. This means that both of these scripts, if they exist, are loaded by the system.</p>
			
				<p>Autoloaded scripts is not necessary on most web projects as long as functionality is built with classes and objects created from those classes that are used by Controllers, Models and Views within Wave Framework and in other JavaScript files. But sometimes it is necessary to load a specific library or libraries or change some PHP configuration setting globally. Autoload script can be used as an option for this.</p>
				
			<h2 id="index-using-autoload-script-for-javascript">Using Autoload Script for JavaScript</h2>
			
				<p>JavaScript autoloaded 'script.js' file is generally considered the master JavaScript file. Most of the simple applications only store scripts in that file alone. This works similarly to any other script file loaded in the system, you just have to keep in mind that this script is loaded after all the external JavaScript libraries are already loaded (this behavior can be changed by editing View Controller).
				
			<h2 id="index-using-autoload-script-with-php">Using Autoload Script with PHP</h2>
				
				<p>For example, if you wish to include a method in your system that is accessible everywhere for checking whether your server environment is 64bit or 32bit, then you can add this to your 'script.php' file:</p>
				
<pre>
	<code>
	function is64bit(){
		$int = "9223372036854775807";
		$int = intval($int);
		if($int==9223372036854775807){
			return true;
		} else if($int==2147483647){
			return false;
		}
	}
	</code>
</pre>

				<p>If this is defined in Autoload script, then this function is now available for all <a href="guide_mvc.htm">MVC</a> objects in the system and can be integrated as part of the web service or website. Everything that is implemented in Autoload will be loaded with every PHP request, so make sure that you only include things that do have to be there with majority of your requests and functionality.</p>
				
				<p>By default the Autoload script is entirely empty and nothing is loaded from it by Wave Framework.</p>
				
				<h3>Usable Variables and Objects</h3>
			
				<p>Autoload is loaded procedurally by Data and API Handlers of Wave Framework. This means that you have access to some of the core aspects of Wave Framework even before API itself is loaded. This also means that you can use the data that is stored by these variables in your Autoload functions, classes or more.</p>
				
				<p>This also means that you should be careful not to rewrite some of these variables, since they may affect the execution of Wave Framework itself. These are the variables that are available by the time Autoload is executed:</p>
				
				<ul>
					<li><b>$state</b> - This holds the <a href="state.htm">WWW_State</a> object and has access to the entire <a href="guide_state.htm">State</a> functionality, from Configuration to sessions and more.</li>
					<li><b>$databaseConnection</b> - This exists only if configuration has <a href="guide_database.htm">State</a> settings set. This holds the <a href="database.htm">WWW_Database</a> object and can be used to make database requests.</li>
					<li><b>$microTime</b> - This stores the microtime from the moment script started executing. It is used by Logger to calculate script execution time.</li>
					<li><b>$resourceAddress, $resourceInfo, $resourceFolder, $resourceFile, $resourceExtension</b> - These are temporary variables that Index Gateway uses to find out what Handler to use. These can be safely overwritten, if needed, by Autoload.</li>
					<li><b>$config</b> - This is the configuration array. Note that it is better to load configuration variables from $state object instead, since State object also adds defaults and does the suitable formatting.</li>
					<li><b>$logger</b> - Only defined if Logger is used, this carries WWW_Logger object.</li>
					<li><b>$limiter</b> - Only defined if Limiter is used, this carries WWW_Limiter object. This is not used by the time Handlers are loaded, thus can be safely overwritten in Autoload.</li>
					<li><b>$apiHandler</b> - Only defined for API Handler, this is the keyword of API handler that is being requested ('json' for json.api, 'xml' for xml.api and so on). This can be safely overwritten by the time Autoload is called.</li>
				</ul>
				
				<p>It is not recommended to modify most of the variables above, but they can be useful especially if you need to make additional database calls or access configuration.</p>
			
	</body>
</html>